package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"grpc_starter/grpc_demo/proto"
	"net"
)

type server struct{}

func (s *server) GetUserInfo(ctx context.Context, req *proto.UserReq) (*proto.UserRes, error) {
	userId := req.UserId
	return &proto.UserRes{
		UserId:   userId,
		UserName: "zhangsan",
		Age:      "10",
	}, nil
}

func (s *server) SayHello(ctx context.Context, req *proto.HelloReq) (*proto.HelloRes, error) {
	return &proto.HelloRes{Msg: "sayHello response data: " + req.Msg}, nil
}

func (s *server) SayName(ctx context.Context, req *proto.NameReq) (*proto.NameRes, error) {
	return &proto.NameRes{Name: "sayName response data: " + req.Name}, nil
}

func main() {
	lis, err := net.Listen("tcp", "127.0.0.1:5000")
	if err != nil {
		panic(err)
	}

	ser := grpc.NewServer()

	proto.RegisterHelloServiceServer(ser, &server{})

	err = ser.Serve(lis)
	if err != nil {
		panic("server start fail: " + err.Error())
	}

	fmt.Println("server is running,address is tpc:127.0.0.1:5000")
}
